using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Adex
{
	/// <summary>
	/// Strongly-typed collection for the Medium class.
	/// </summary>
	[Serializable]
	public partial class MediumCollection : ActiveList<Medium, MediumCollection> 
	{	   
		public MediumCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Medium table.
	/// </summary>
	[Serializable]
	public partial class Medium : ActiveRecord<Medium>
	{
		#region .ctors and Default Settings
		
		public Medium()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Medium(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Medium(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Medium(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Medium", TableType.Table, DataService.GetInstance("Adex"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarMediaID = new TableSchema.TableColumn(schema);
				colvarMediaID.ColumnName = "MediaID";
				colvarMediaID.DataType = DbType.Int32;
				colvarMediaID.MaxLength = 0;
				colvarMediaID.AutoIncrement = true;
				colvarMediaID.IsNullable = false;
				colvarMediaID.IsPrimaryKey = false;
				colvarMediaID.IsForeignKey = false;
				colvarMediaID.IsReadOnly = false;
				colvarMediaID.DefaultSetting = @"";
				colvarMediaID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMediaID);
				
				TableSchema.TableColumn colvarMediaCode = new TableSchema.TableColumn(schema);
				colvarMediaCode.ColumnName = "MediaCode";
				colvarMediaCode.DataType = DbType.Int32;
				colvarMediaCode.MaxLength = 0;
				colvarMediaCode.AutoIncrement = false;
				colvarMediaCode.IsNullable = false;
				colvarMediaCode.IsPrimaryKey = true;
				colvarMediaCode.IsForeignKey = false;
				colvarMediaCode.IsReadOnly = false;
				colvarMediaCode.DefaultSetting = @"";
				colvarMediaCode.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMediaCode);
				
				TableSchema.TableColumn colvarMediaName = new TableSchema.TableColumn(schema);
				colvarMediaName.ColumnName = "MediaName";
				colvarMediaName.DataType = DbType.String;
				colvarMediaName.MaxLength = 255;
				colvarMediaName.AutoIncrement = false;
				colvarMediaName.IsNullable = true;
				colvarMediaName.IsPrimaryKey = false;
				colvarMediaName.IsForeignKey = false;
				colvarMediaName.IsReadOnly = false;
				colvarMediaName.DefaultSetting = @"";
				colvarMediaName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMediaName);
				
				TableSchema.TableColumn colvarMediaType = new TableSchema.TableColumn(schema);
				colvarMediaType.ColumnName = "MediaTypeCode";
				colvarMediaType.DataType = DbType.Int32;
				colvarMediaType.MaxLength = 0;
				colvarMediaType.AutoIncrement = false;
				colvarMediaType.IsNullable = true;
				colvarMediaType.IsPrimaryKey = false;
				colvarMediaType.IsForeignKey = true;
				colvarMediaType.IsReadOnly = false;
				colvarMediaType.DefaultSetting = @"";
				
					colvarMediaType.ForeignKeyTableName = "MediaType";
				schema.Columns.Add(colvarMediaType);
				
				TableSchema.TableColumn colvarParentMediaID = new TableSchema.TableColumn(schema);
				colvarParentMediaID.ColumnName = "ParentMediaID";
				colvarParentMediaID.DataType = DbType.Int32;
				colvarParentMediaID.MaxLength = 0;
				colvarParentMediaID.AutoIncrement = false;
				colvarParentMediaID.IsNullable = true;
				colvarParentMediaID.IsPrimaryKey = false;
				colvarParentMediaID.IsForeignKey = false;
				colvarParentMediaID.IsReadOnly = false;
				colvarParentMediaID.DefaultSetting = @"";
				colvarParentMediaID.ForeignKeyTableName = "";
				schema.Columns.Add(colvarParentMediaID);
				
				TableSchema.TableColumn colvarDateAdded = new TableSchema.TableColumn(schema);
				colvarDateAdded.ColumnName = "DateAdded";
				colvarDateAdded.DataType = DbType.DateTime;
				colvarDateAdded.MaxLength = 0;
				colvarDateAdded.AutoIncrement = false;
				colvarDateAdded.IsNullable = false;
				colvarDateAdded.IsPrimaryKey = false;
				colvarDateAdded.IsForeignKey = false;
				colvarDateAdded.IsReadOnly = false;
				
						colvarDateAdded.DefaultSetting = @"(getdate())";
				colvarDateAdded.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateAdded);
				
				TableSchema.TableColumn colvarDateModified = new TableSchema.TableColumn(schema);
				colvarDateModified.ColumnName = "DateModified";
				colvarDateModified.DataType = DbType.DateTime;
				colvarDateModified.MaxLength = 0;
				colvarDateModified.AutoIncrement = false;
				colvarDateModified.IsNullable = false;
				colvarDateModified.IsPrimaryKey = false;
				colvarDateModified.IsForeignKey = false;
				colvarDateModified.IsReadOnly = false;
				
						colvarDateModified.DefaultSetting = @"(getdate())";
				colvarDateModified.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateModified);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["Adex"].AddSchema("Medium",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("MediaID")]
		public int MediaID 
		{
			get { return GetColumnValue<int>("MediaID"); }

			set { SetColumnValue("MediaID", value); }

		}

		  
		[XmlAttribute("MediaCode")]
		public int MediaCode 
		{
			get { return GetColumnValue<int>("MediaCode"); }

			set { SetColumnValue("MediaCode", value); }

		}

		  
		[XmlAttribute("MediaName")]
		public string MediaName 
		{
			get { return GetColumnValue<string>("MediaName"); }

			set { SetColumnValue("MediaName", value); }

		}

		  
		[XmlAttribute("MediaType")]
		public int? MediaType 
		{
			get { return GetColumnValue<int?>("MediaTypeCode"); }

			set { SetColumnValue("MediaTypeCode", value); }

		}

		  
		[XmlAttribute("ParentMediaID")]
		public int? ParentMediaID 
		{
			get { return GetColumnValue<int?>("ParentMediaID"); }

			set { SetColumnValue("ParentMediaID", value); }

		}

		  
		[XmlAttribute("DateAdded")]
		public DateTime DateAdded 
		{
			get { return GetColumnValue<DateTime>("DateAdded"); }

			set { SetColumnValue("DateAdded", value); }

		}

		  
		[XmlAttribute("DateModified")]
		public DateTime DateModified 
		{
			get { return GetColumnValue<DateTime>("DateModified"); }

			set { SetColumnValue("DateModified", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Adex.CommercialLogCollection CommercialLogRecords()
		{
			return new Adex.CommercialLogCollection().Where(CommercialLog.Columns.MediaCode, MediaCode).Load();
		}

		public Adex.InternetLogCollection InternetLogRecords()
		{
			return new Adex.InternetLogCollection().Where(InternetLog.Columns.MediaCode, MediaCode).Load();
		}

		public Adex.NewsLogCollection NewsLogRecords()
		{
			return new Adex.NewsLogCollection().Where(NewsLog.Columns.MediaCode, MediaCode).Load();
		}

		public Adex.OutdoorLogCollection OutdoorLogRecords()
		{
			return new Adex.OutdoorLogCollection().Where(OutdoorLog.Columns.MediaCode, MediaCode).Load();
		}

		public Adex.PlanningLogCollection PlanningLogRecords()
		{
			return new Adex.PlanningLogCollection().Where(PlanningLog.Columns.MediaCode, MediaCode).Load();
		}

		public Adex.PressLogCollection PressLogRecords()
		{
			return new Adex.PressLogCollection().Where(PressLog.Columns.MediaCode, MediaCode).Load();
		}

		public Adex.ProgramCollection ProgramRecords()
		{
			return new Adex.ProgramCollection().Where(Program.Columns.MediaCode, MediaCode).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a MediaType ActiveRecord object related to this Medium
		/// 
		/// </summary>
		public Adex.MediaType MediaType
		{
			get { return Adex.MediaType.FetchByID(this.MediaType); }

			set { SetColumnValue("MediaTypeCode", value.MediaTypeX); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varMediaCode,string varMediaName,int? varMediaType,int? varParentMediaID,DateTime varDateAdded,DateTime varDateModified)
		{
			Medium item = new Medium();
			
			item.MediaCode = varMediaCode;
			
			item.MediaName = varMediaName;
			
			item.MediaType = varMediaType;
			
			item.ParentMediaID = varParentMediaID;
			
			item.DateAdded = varDateAdded;
			
			item.DateModified = varDateModified;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varMediaID,int varMediaCode,string varMediaName,int? varMediaType,int? varParentMediaID,DateTime varDateAdded,DateTime varDateModified)
		{
			Medium item = new Medium();
			
				item.MediaID = varMediaID;
				
				item.MediaCode = varMediaCode;
				
				item.MediaName = varMediaName;
				
				item.MediaType = varMediaType;
				
				item.ParentMediaID = varParentMediaID;
				
				item.DateAdded = varDateAdded;
				
				item.DateModified = varDateModified;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string MediaID = @"MediaID";
			 public static string MediaCode = @"MediaCode";
			 public static string MediaName = @"MediaName";
			 public static string MediaType = @"MediaTypeCode";
			 public static string ParentMediaID = @"ParentMediaID";
			 public static string DateAdded = @"DateAdded";
			 public static string DateModified = @"DateModified";
						
		}

		#endregion
	}

}

